package com.fallwings.Controller;

import com.fallwings.Service.IUserService;
import com.fallwings.domain.User;
import com.fallwings.domain.UserInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * Created by Killer on 2015/12/16.
 */
@Controller

public class UserController {

    @Resource(name = "userService")
    private IUserService iUserService;

    /**
     * 用户登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public ResponseEntity<User> Login(User user, HttpSession session) {
        User checkUser = iUserService.checkUser(user);
        if (checkUser != null) {
            session.setAttribute("user", checkUser);
            return new ResponseEntity<User>(checkUser, HttpStatus.OK);
        } else {
            return new ResponseEntity<User>(HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 检查是否已经登陆
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/login/checkLogin")
    public ResponseEntity<User> checkLogin(HttpSession session){
        User user=(User)session.getAttribute("user");
        if (user==null){
            return new ResponseEntity<User>(HttpStatus.UNAUTHORIZED);
        }else {
            return new ResponseEntity<User>(user,HttpStatus.OK);
        }
    }

    /**
     * 注销账号，销毁当前回话
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/loginOut")
    public ResponseEntity<String> DestorySession(HttpSession session){
        session.invalidate();
        return new ResponseEntity<String>(HttpStatus.OK);
    }
    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/register")
    public ResponseEntity<String> Register(User user,UserInfo userInfo) {
        User userCheck = iUserService.userCheck(user);
        if (userCheck==null) {//用户不存在
            iUserService.register(user);
            iUserService.addUserinfo(userInfo,user);
            return new ResponseEntity<String>(HttpStatus.OK);

        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 查询用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/user/userInfo")
    public ResponseEntity<UserInfo> selectUserInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return new ResponseEntity<UserInfo>(iUserService.selectUserinfo(user), HttpStatus.OK);
    }

    /**
     * 修改用户密码
     *
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/user/updateUserpassword")
    public ResponseEntity<String> updateUserpassword(User user) {
        iUserService.updatePassword(user);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    /**
     * 修改用户权限
     *
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/user/updateRoot")
    public ResponseEntity<String> updateRoot(User user) {
        iUserService.updateRoot(user);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    /**
     * 修改用户信息(目前只能修改用户头像！)
     *
     * @param userInfo
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/user/updateUserinfo")
    public ResponseEntity<String> updateUserinfo(UserInfo userInfo) {
        iUserService.updateUserinfo(userInfo);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    /**
     * 修改用户积分
     *
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/user/updateIntegral")
    public ResponseEntity<String> updateIntegral(User user) {
        iUserService.updateIntegral(user);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    /**
     * 增加用户信息(用户之前并没有编写录入过信息)
     *
     * @param userInfo
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/user/addUserinfo")
    public ResponseEntity<String> addUserinfo(UserInfo userInfo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        iUserService.addUserinfo(userInfo, user);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

}
